<%@page import="systems.AdsSystem"%>
<%@page import="helpers.LanguageHelper"%>
<%@page import="helpers.DateHelper"%>
<%@page import="entities.Category"%>
<%@page import="entities.User"%>
<%@page import="java.util.Map"%>
<%@page import="helpers.PaginatorHelper"%>
<%@page import="helpers.StringHelper"%>
<%@page import="entities.Article"%>
<%@page import="java.util.List"%>
<%
List<Article> articles = (List<Article>) request.getAttribute("articles"); 
Map<String, User> authors = (Map<String, User>) request.getAttribute("authors");
Map<String, Category> categories = (Map<String, Category>) request.getAttribute("categories");
User author = null;
Category category = null;
LanguageHelper translate = (LanguageHelper) request.getAttribute("translate");

//Paginator
int pages = (Integer) request.getAttribute("page");
boolean nextPage = (Boolean) request.getAttribute("nextPage");
boolean prevPage = (Boolean) request.getAttribute("prevPage");
PaginatorHelper paginator = (PaginatorHelper) request.getAttribute("paginator");

%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<jsp:include page="/layouts/header.jsp" />
            <div id="mainContent" class="left" itemscope itemtype="http://schema.org/ItemList">
                <h2 class="title" itemprop="name"><%=translate.getString("lastest_article")%></h2>
                <meta itemprop="mainContentOfPage" content="true"/>
				<meta itemprop="itemListOrder" content="Descending" />
				<% 
                for (int i = 0; i < articles.size(); i++) {
                	category = categories.get(articles.get(i).getCategory());
                	author = authors.get(articles.get(i).getCreator());
                %>
                <article class="articleItem" itemscope itemtype="http://schema.org/Article" itemprop="itemListElement">
                    <h2 class="articleTitle" itemprop="name">
                    	<a href="/a/<%=articles.get(i).getAlias() %>"><%=articles.get(i).getTitle() %></a>
                    </h2>
                    <div class="articleMeta clearafter">
                    	<% if (author != null) { %>
                        <%=translate.getString("by")%>&nbsp;<a href="/u/<%=author.getId()%>" itemprop="author"><%=author.getName() %></a> | 
                        <% } else if (!articles.get(i).getAuthorName().isEmpty()) { %>
                        <%=translate.getString("by")%>&nbsp;<%=articles.get(i).getAuthorName() %> | 
                        <% } %>
						<%=translate.getString("on")%>&nbsp;<span itemprop="dateCreated"><%=DateHelper.getStringFromDate(articles.get(i).getCreated(), "MMM dd, yyyy") %></span>  
                        
                        <% if (category != null) { %>
                        <a href="/c/<%=category.getAlias() %>" class="articleCat right"><%=category.getName() %></a>
                        <% } %>
                        <meta itemprop="interactionCount" content="UserComments:5"/>
                    </div>
                    <div class="entry clearafter" itemprop="description">
                    <% if (i < 3) { %>
                    <div class="adv">
						<%=AdsSystem.getAds468x15() %>
	                </div>
	                <% } %>
                        <p><%=StringHelper.cutString(StringHelper.html2text(articles.get(i).getContent()), 40) %>...</p>
                    </div>
                    <a href="/a/<%=articles.get(i).getAlias() %>" class="readMore"><%=translate.getString("readmore") %></a>
                </article>
                <% if (i == 3) { %>
                <article>
                	<div class="adv content-center">
			        	<%=AdsSystem.getAds468x60() %>
			        </div>
                </article>
                <% } %>
                <% } %>
                
                <ul class="pagination">
                    <%
					List<Integer> range = paginator.getRangePage();
					%>
                    <% if (prevPage) { %>
					<li><a href="<%=paginator.getPrev()%>"><%=translate.getString("prev")%></a></li>
					<% } %>
					<%
					if (nextPage || prevPage) {
						for (int i = 0; i < range.size(); i++) {
					%>
					<li><a class="<%=range.get(i).intValue() == pages ? "current" : "" %>" href="<%=range.get(i).intValue() == pages ? "javascript: return false;" : paginator.getPage(range.get(i).intValue())%>"> <%=range.get(i).toString() %> </a></li>
					<%  }
					} %>
					
					<% if (nextPage) { %>
					<li><a href="<%=paginator.getNext()%>"> <%=translate.getString("next")%> </a></li>
					<% } %>
                </ul>
            </div>
            
            <jsp:include page="/elements/right.jsp" />
            
            <div class="clearbreak"></div>
<jsp:include page="/layouts/footer.jsp" />